package com.net2s.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jfree.util.Log;

import com.net2s.bbdd.UtilSQL;
import com.net2s.utils.Messages;

/**
 * Servlet implementation class ServletModifyUser
 */
public class ServletModifyUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
	static Logger log= Logger.getLogger(ServletModifyUser.class);   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletModifyUser() {
        super();
        // TODO Auto-generated constructor stub
    	
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		procesaPeticion(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		procesaPeticion(request, response);
	}
	

	private void procesaPeticion(HttpServletRequest request, HttpServletResponse response){
		if((request.getParameter("newmarketing")!=null) && ((request.getParameter("newconsulta")!=null))){
			try{
				request.getSession().setAttribute("mensajebaja", Messages.getString("ModifyUser.ProfilesError"));
				response.sendRedirect(response.encodeRedirectURL("ListUser.jsp"));
			}catch(Exception e){
				log.error(e);
			}
		}
		else{	

			request.getSession().setAttribute("mensajebaja", null);
			String query = "update users set username='" + request.getParameter("username") + "', password='" + request.getParameter("password") + "'," +
			"nombre='" + request.getParameter("nombre") + "', apellidos='" + request.getParameter("apellidos") + "'";  
			if(request.getParameter("newconsulta")!=null)
				query = query + " ,profile='consulta'";
			if(request.getParameter("newmarketing")!=null)
				query = query + " ,profile='marketing'";
			
			if(request.getParameter("newadministrador")==null)
				query = query + " ,admin='N'";
			else if(request.getParameter("newadministrador")!=null)
				query = query + " ,admin='Y'";
			
			query = query + " where id=" + request.getSession().getAttribute("id");
			request.getSession().setAttribute("id", null);
			
			log.info("Nombre vale " + request.getParameter("nombre"));
			try{
				if((request.getParameter("newconsulta")==null) && (request.getParameter("newmarketing")==null)){
					request.getSession().setAttribute("mensajebaja", Messages.getString("ModifyUser.PerfilesBlanco"));
					response.sendRedirect(response.encodeRedirectURL("ListUser.jsp"));
				}
				else if((request.getParameter("nombre")=="") || (request.getParameter("username")=="") || (request.getParameter("password")=="") || (request.getParameter("apellidos")=="")){
					request.getSession().setAttribute("mensajebaja", Messages.getString("ModifyUser.Blancos"));
					response.sendRedirect(response.encodeRedirectURL("ListUser.jsp"));
				}
				else{
					UtilSQL.executeQuery(query);
					request.getSession().setAttribute("mensajebaja", Messages.getString("ModifyUser.OK"));
				}
			}catch(Exception e){
				log.error(e);
				request.getSession().setAttribute("mensajebaja",e.getMessage() );
			}
			
			try{
				response.sendRedirect(response.encodeRedirectURL("ListUser.jsp"));
			}catch(Exception e){
				log.error(e);
			}

		}
	}

}

